import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../../common/my_icon.dart';
import '../../../components/status/empty.dart';
import '../../../config/alarm_type_config.dart';
import '../../../config/global_config.dart';
import '../../../model/bean/alarm_bean.dart';
import '../../../utils/screenutil/fit.dart';
import '../../../utils/tools/Tools.dart';

class AlarmList extends StatelessWidget {
  AlarmListBean listData;
  Function seeMore;
  Function seeItem;

  /// 告警列表
  /// * [listData]
  /// * [seeMore] Function|查看更多|选填
  /// * [seeItem] Function|查看每一项的内容|选填
  AlarmList(AlarmListBean listData, {Function seeMore, Function seeItem})
      : this.listData = listData,
        this.seeMore = seeMore,
        this.seeItem = seeItem;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.f),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.f),
      ),
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.only(
              top: 16.f,
              bottom: 10.f,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "最近告警",
                  style: TextStyle(
                    fontSize: 28.f,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
          Container(
            child: listData.list != null && listData.list.length > 0
                ? Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: createWidgets(
                        listData.list != null ? listData.list.length : 0),
                  )
                : Empty(tip: "最近没有告警数据"),
          )
        ],
      ),
    );
  }

  /// 创建多个 Widget
  /// * [total] int|数据长度|必填
  List<Widget> createWidgets(int total) {
    var _data = listData.list;
    List<Widget> lw = [];
    for (int i = 0; i < total; i++) {
      lw.add(
        InkWell(
          onTap: () => seeItem({
            "monitorId": listData.list[i].boxId,
            "communityName": listData.list[i].villageName,
            "liftId": listData.list[i].elevatorId,
            "liftLocation":
                "${listData.list[i].buildingNo}${listData.list[i].unitNo}${listData.list[i].shaft}",
          }),
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 15.f),
            decoration: BoxDecoration(
              border: total - 1 == i
                  ? null
                  : Border(
                      bottom: BorderSide(
                        width: 1.f,
                        color: GlobalConfig.borderColor,
                      ),
                    ),
            ),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: 60.f,
                  height: 60.f,
                  margin: EdgeInsets.only(right: 10.f),
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.circular(60.f),
                  ),
                  child: Icon(
                    MyIcon.gg,
                    size: 34.f,
                    color: Colors.white,
                  ),
                ),
                Container(
                  width: 320.f,
                  margin: EdgeInsets.only(right: 10.f),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "${AlarmTypeConfig.type[_data[i].type]}",
                        style: TextStyle(
                          color: GlobalConfig.titleColor,
                          fontSize: 24.f,
                        ),
                        softWrap: true,
                        textAlign: TextAlign.left,
                        overflow: TextOverflow.ellipsis,
                      ),
                      SizedBox(height: 15.f),
                      Text(
                        "${_data[i].villageName}/${_data[i].buildingNo}/${_data[i].unitNo}/${_data[i].shaft}号电梯井",
//                        softWrap: true,
//                        textAlign: TextAlign.left,
//                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          color: GlobalConfig.assistColor,
                          fontSize: 20.f,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  alignment: Alignment.topRight,
                  width: 160.f,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Text(
                        "${_data[i].status == 1 ? '已处理' : '未处理'}",
                        style: TextStyle(
                          color:
                              _data[i].status == 1 ? Colors.blue : Colors.red,
                          fontSize: 24.f,
                        ),
                      ),
                      SizedBox(height: 15.f),
                      Text(
                        "${Tools.dateFormat(DateTime.parse(_data[i].createTime), DataFormats.mo_d_h_m)}",
                        style: TextStyle(
                          color: GlobalConfig.assistColor,
                          fontSize: 20.f,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  width: 40.f,
                  alignment: Alignment.centerRight,
                  child: Icon(
                    Icons.chevron_right,
                    color: GlobalConfig.assistColor,
                    size: 40.f,
                  ),
                )
              ],
            ),
          ),
        ),
      );
    }
    return lw;
  }
}
